//
//  DHLoginGenderViewController.m
//  Dresshere
//
//  Created by ZhangPeng on 2017/12/21.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHLoginGenderViewController.h"
#import "DHHomeViewController.h"

@interface DHLoginGenderViewController ()

@property (weak, nonatomic) IBOutlet UIButton *menButton;
@property (weak, nonatomic) IBOutlet UIButton *womenButton;
@property (weak, nonatomic) IBOutlet UIButton *closeButton;

@end

@implementation DHLoginGenderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.womenButton.layer.borderWidth = 1.0;
    self.womenButton.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.menButton.layer.borderWidth = 1.0;
    self.menButton.layer.borderColor = [[UIColor whiteColor] CGColor];

    [self buttonTouchActions];
}

- (void)closeController{
    if (self.userManagerPresent) {
        [self dismissViewControllerAnimated:YES completion:nil];
    }else{
        [kAppDelegate configHomeController];
    }
}

- (void)buttonTouchActions{
    WeakSelf(weakSelf)
    [self.closeButton bk_addEventHandler:^(id sender) {
        [weakSelf closeController];
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.womenButton bk_addEventHandler:^(id sender) {
        if ([Login isLogin]) {
            [[DHNetAPIClient shareDHNetAPIClient] request_user_sexWithParams:@{@"sex":@"女"} andBlock:^(id data, NSError *error) {
                [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                    [Login curLoginUser].sex = @"女";
                    [Login doLogin:[Login curLoginUser]];
                    [weakSelf closeController];
                    postN(@"reloadSearchCategory");
                }];
            }];
        }else{
            [[NSUserDefaults standardUserDefaults] setObject:@"女" forKey:USER_SELECT_SEX];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [kAppDelegate configHomeController];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }
    } forControlEvents:UIControlEventTouchUpInside];
    
    [self.menButton bk_addEventHandler:^(id sender) {
        if ([Login isLogin]) {
            [[DHNetAPIClient shareDHNetAPIClient] request_user_sexWithParams:@{@"sex":@"男"} andBlock:^(id data, NSError *error) {
                [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
                    [Login curLoginUser].sex = @"男";
                    [Login doLogin:[Login curLoginUser]];
                    [weakSelf closeController];
                    postN(@"reloadSearchCategory");
                }];
            }];
        }else{
            [[NSUserDefaults standardUserDefaults] setObject:@"男" forKey:USER_SELECT_SEX];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [kAppDelegate configHomeController];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
